﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace WQDSkin.Forms.Controls
{
    public class WQDTreeViewItem : TreeViewItem
    {

        public override void OnApplyTemplate()
        {
            
            base.OnApplyTemplate();

            //FrameworkElement ItemsHost = (FrameworkElement)this.Template.FindName("ItemsHost", this);
            //ItemsHost.Margin = new Thickness(this.ActualHeight);
            WQDTreeView tv = this.ParentTreeView;
            if (tv != null)
            { 
                if (tv.Icon != null && this.Icon == null)
                {
                    this.Icon = tv.Icon;
                }
            }
        }

        private WQDTreeView ParentTreeView
        {
            get
            {
                ItemsControl parent = ParentItemsControl;
                while (parent != null)
                {
                    WQDTreeView tv = parent as WQDTreeView;
                    if (tv != null)
                    {
                        return tv;
                    }

                    parent = ItemsControl.ItemsControlFromItemContainer(parent);
                }

                return null;
            }
        }
 
        /// <summary>
        ///     Returns the immediate parent ItemsControl.
        /// </summary>
        private ItemsControl ParentItemsControl
        {
            get
            {
                return ItemsControl.ItemsControlFromItemContainer(this);
            }
        }

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
            
        }

        [Description("图标"), Category("WQDSkin")]
        public Geometry Icon
        {
            get { return (Geometry)GetValue(IconProperty); }
            set { SetValue(IconProperty, value); }
        }
        public static readonly DependencyProperty IconProperty =
            DependencyProperty.Register("Icon", typeof(Geometry), typeof(WQDTreeViewItem), new PropertyMetadata(null));

        public double IconWidth
        {
            get { return (double)GetValue(IconWidthProperty); }
            set { SetValue(IconWidthProperty, value); }
        }
        public static readonly DependencyProperty IconWidthProperty =
            DependencyProperty.Register("IconWidth", typeof(double), typeof(WQDTreeViewItem), new PropertyMetadata(12.0));

        /// <summary>
        /// 图标高度
        /// </summary>
        public double IconHeight
        {
            get { return (double)GetValue(IconHeightProperty); }
            set { SetValue(IconHeightProperty, value); }
        }
        public static readonly DependencyProperty IconHeightProperty =
            DependencyProperty.Register("IconHeight", typeof(double), typeof(WQDTreeViewItem), new PropertyMetadata(12.0));


        protected override DependencyObject GetContainerForItemOverride()
        {
            return new WQDTreeViewItem();
        }
    }
}
